//
//  YZMImageService.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/18.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMImageService.h"

@implementation YZMImageService

+ (NSString *)pathInDocumentDirectory:(NSString *)fileName{
    
    //    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [documentPaths objectAtIndex:0];
    return [documentPath stringByAppendingPathComponent:fileName];
}

+ (BOOL)hasDir:(NSString *)filePath  bDir:(BOOL)isDir{
    
    BOOL bExist = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
    if (existed){
        
        bExist = YES;
    }
    return bExist;
}

+ (BOOL)createDirInDocument:(NSString *)dirName{
    
    NSString *dir = [[self class] pathInDocumentDirectory:dirName];
    BOOL isDir = YES;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:dir isDirectory:&isDir];
    bool isCreated = false;
    if (!existed){
        
        isCreated = [fileManager createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return isCreated;
}

+ (BOOL)deleteDirInDocument:(NSString *)dirName isDir:(BOOL)bDir{
    
    NSString *imageDir = [self pathInDocumentDirectory:dirName];
    BOOL isDir = bDir;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
    bool isDeleted = false;
    if ( isDir == YES && existed == YES ){
        
        isDeleted = [fileManager removeItemAtPath:imageDir error:nil];
    }
    
    return isDeleted;
}

+ (BOOL)saveImageToDocument:(NSString *)directoryPath image:(UIImage *)image  imageName:(NSString *)imageName
{
    BOOL isDir = YES;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self class] pathInDocumentDirectory:directoryPath];
    BOOL existed = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    bool isSaved = false;
    if (!existed) {
        
        if ([[self class] createDirInDocument:directoryPath]) {
            existed = YES;
        }
    }
    
    if ( isDir == YES && existed == YES ){
        
        isSaved = [UIImagePNGRepresentation(image) writeToFile:[path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", imageName]] options:NSAtomicWrite error:nil];
    }
    return isSaved;
}

+(NSData *)loadImageData:(NSString *)directoryPath fileName: (NSString *)fileName isDir:(BOOL)bDir{
    BOOL isDir = bDir;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self class] pathInDocumentDirectory:directoryPath];
    BOOL dirExisted = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    if (dirExisted == YES ){
        
        NSString *imagePath = [path stringByAppendingPathComponent:fileName];
        BOOL fileExisted = [fileManager fileExistsAtPath:imagePath];
        if (!fileExisted) {
            return nil;
        }
        NSData *data = [NSData dataWithContentsOfFile:imagePath];
        return data;
    }
    else{
        
        return nil;
    }
}

@end
